/*
 * Created by Federico Pisarello - SisDev Software (c) 2012 - fpisarello@sisdevsoft.com
 */

var popForm = 'measurementPop.php';
var popResultForm = 'measurementResponsePop.php';
var actionForm = 'measurementAction.php';

var joDataMeasurement;
var oTable;

/** Variable of tracking the IndexPK **/
var actionPK = undefined;
var action = undefined;

function $_init()
{
	var gaiSelected;
	
	$(document).ready(function() {
    	oTable = $('#dt_measurement').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"bLengthChange": false,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=search",
					"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
						if ( jQuery.inArray(aData[0], gaiSelected) != -1 )
						{
					        $(nRow).addClass('row_selected');
						}
						return nRow;
					},
					"aoColumns": [
					  			null,
					  			null,
					  			null,
					  			null,
					  			null,
					  			null
					  		]

				});

    	/* Click event handler */
    	$('#dt_measurement tbody tr').live('click', function () {
    		var aData = oTable.fnGetData(this);
    		var iId = aData[0];
    		actionPK = aData[0];
    		
    		gaiSelected =  [];
    		if ( $(this).hasClass('row_selected') ) {
	            $(this).removeClass('row_selected');
	            actionPK = undefined;
	        }
	        else {
	            oTable.$('tr.row_selected').removeClass('row_selected');
	            $(this).addClass('row_selected');
	            gaiSelected[gaiSelected.length++] = iId;
	        }
    	} );

	});
	
	$("#measurementDialog").dialog({
		autoOpen:false, 
		height:'auto', 
		width:600, 
		modal: true,
		resizable: false,
		closeOnEscape: false,
		position : ['center',2],		
		dialogClass:'no-close',
		beforeClose: function() { $_ClearMeasurementPopForm(); },
	});
	
	$("#measurementResultDialog").dialog({
		autoOpen:false, 
		height:'auto', 
		width:700, 
		modal: true,
		resizable: false,
		closeOnEscape: false,
		position : ['center',2],		
		dialogClass:'no-close'
	});
	
	//Add Click for All Buttons in Search Form
	$('#btnViewValues').click( function(e) {
		viewProxyShow();
	});	
	
	$('#btnViewResult').click( function(e) {
		viewResultProxyShow();
	});	
}

function $_initPop()
{	
	//Add Click for All Buttons in Pop Form
	$('#btnExit').click( function(e) {
		$('#measurementDialog').dialog('close');
	});
		
	//Populate Horses Data	
	if(action == iConst.EDIT)
	{
		$('#horseName').val(joDataMeasurement.horseName);
		$('#initTime').val(joDataMeasurement.initTime);
		$('#finishTime').val(joDataMeasurement.finishTime);
		$('#lacticAcid1').val(joDataMeasurement.lacticAcid1);
		$('#heartRate1').val(joDataMeasurement.heartRate1);
		$('#lacticAcid2').val(joDataMeasurement.lacticAcid2);
		$('#heartRate2').val(joDataMeasurement.heartRate2);
		$('#lacticAcid3').val(joDataMeasurement.lacticAcid3);
		$('#heartRate3').val(joDataMeasurement.heartRate3);
		$('#respiratoryRate3').val(joDataMeasurement.respiratoryRate3);
		$('#work').val(joDataMeasurement.work);
		$('#meters').val(joDataMeasurement.meters);
		$('#time').val(joDataMeasurement.time);
	}
}

function viewProxyShow()
{
	if(actionPK === undefined)
	{
		$_showMessage('ALERT', 'ALERTA', strMenssageSelectionRecordInGrid);
		return;
	}
	
	action = iConst.EDIT;
	var paramData =  "actionOfForm=" + iConst.VIEW + "&idMeasurementItem=" + actionPK;
	
    $.ajax({
        type: 'POST',
        url: actionForm,
        data: paramData,
		dataType: 'json',
        success: function(jsonObj)
                            {
        						joDataMeasurement = jsonObj;
        						//Open Pop Dialog
        						$('#measurementDialog').load(popForm).dialog('open');
                            }
        });
	
return true;
}

function viewResultProxyShow()
{
	if(actionPK === undefined) {
		$_showMessage('ALERT', 'ALERTA', strMenssageSelectionRecordInGrid);
		return;
	}
		
    $('#measurementResultDialog').load(popResultForm + "?idMeasurementItem=" + actionPK).dialog('open');
	
return true;
}

function $_ClearMeasurementPopForm()
{	
	$("#measurementDialog").empty();
}